 <?php
class inventoryModel extends RelationModel
{
    protected $_link = array(
        //
        'model' => array(
            'mapping_type' => BELONGS_TO,
            'class_name' => 'model',
            'foreign_key' => 'modelId',
        ),
        'region' => array(
            'mapping_type' => BELONGS_TO,
            'class_name' => 'region',
            'foreign_key' => 'regionId',
        ),  
        'store'=>array(
            'mapping_type' => BELONGS_TO,
            'class_name' => 'store',
            'foreign_key' => 'storeId',        
        ),      
    );
    
    protected $_auto = array (    
        array('customerId','getAdminId',1,'function'), 
        array('customerUpdateTime','time',3,'function') , 
        array('salesId','getUserId',3,'function'),
        array('salesUpdateTime','time',3,'function'), 
    );
    
	/**
	 * 查找库存信息
	 */
	public function seachInventory($where){
		$field = array(
			'i.brandId',
			'b.name' => 'brandName',
			'i.modelId',
			'm.name' => 'modelName',
            'SUM( i.customerInventory)'=>'customerInventory',
            'SUM( i.salesInventory)'=>'salesInventory',
            'c.id'=>'cityId',
            'a.id'=>'areaId',
		);
		$list = D('inventory')->field($field)->alias('i')
				->join(C('DB_PREFIX')."model AS m ON m.id = i.modelId")
				->join(C('DB_PREFIX')."brand AS b ON b.id = m.brandId")
				->join(C('DB_PREFIX')."region AS r ON r.id = i.regionId")
				->join(C('DB_PREFIX')."region AS c ON c.id = r.pid")
				->join(C('DB_PREFIX')."region AS a ON a.id = c.pid")
				->group("i.modelId")
				->order("brandName,modelName")
				->where($where)->select();
		return $list;
	}
}